﻿namespace Sgr.BillNumbering
{
    /// <summary>
    /// 编码规则定义
    /// </summary>
    public class CodeRule
    {
        /// <summary>
        /// 规则名称(唯一标识)
        /// </summary>
        public string RuleName { get; set; } = string.Empty;

        /// <summary>
        /// 策略名称(对应编码策略)
        /// </summary>
        public string StrategyName { get; set; } = "Default";

        /// <summary>
        /// 业务编码前缀（如：SO、PO、INV 等）
        /// </summary>
        public string BusinessCode { get; set; } = string.Empty;

        /// <summary>
        /// 业务类型代码（如：STD、URG、RET 等，用于进一步细分业务类型）
        /// </summary>
        public string BusinessTypeCode { get; set; } = string.Empty;

        /// <summary>
        /// 是否包含校验位
        /// </summary>
        public bool IncludeCheckDigit { get; set; } = false;

        /// <summary>
        /// 序列长度(不足时左补零)
        /// </summary>
        public int SequenceLength { get; set; } = 6;

        /// <summary>
        /// 序列重置策略
        /// </summary>
        public SequenceResetStrategy ResetStrategy { get; set; } = SequenceResetStrategy.Week;

        /// <summary>
        /// 序列段大小(每次分配的序列号数量)
        /// </summary>
        public int? SegmentSize { get; set; } = 50;

        /// <summary>
        /// 序列溢出策略
        /// </summary>
        public SequenceOverflowStrategy SequenceOverflow { get; set; } = SequenceOverflowStrategy.Extend;

        /// <summary>
        /// 分隔符(默认为空)
        /// </summary>
        public string Separator { get; set; } = string.Empty;

        /// <summary>
        /// 自定义参数(策略扩展用)
        /// </summary>
        public Dictionary<string, string> CustomParameters { get; set; } = new();

        /// <summary>
        /// 获取自定义参数值
        /// </summary>
        /// <param name="key">参数键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>参数值或默认值</returns>
        public string GetCustomParameter(string key, string defaultValue = "")
        {
            if (CustomParameters.TryGetValue(key, out var value))
                return value;
            return defaultValue;
        }

        /// <summary>
        /// 设置自定义参数值
        /// </summary>
        /// <param name="key">参数键</param>
        /// <param name="value">参数值</param>
        public void SetCustomParameter(string key, string value)
        {
            CustomParameters[key] = value;
        }

        /// <summary>
        /// 克隆当前规则
        /// </summary>
        /// <returns>CodeRule 的副本</returns>
        public CodeRule Clone()
        {
            return new CodeRule
            {
                RuleName = RuleName,
                StrategyName = StrategyName,
                BusinessCode = BusinessCode,
                BusinessTypeCode = BusinessTypeCode,
                IncludeCheckDigit = IncludeCheckDigit,
                SequenceLength = SequenceLength,
                Separator = Separator,
                CustomParameters = new Dictionary<string, string>(CustomParameters)
            };
        }
    }
}